package com.goldgov.authorityInfo.web;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.gtiles.components.securityworkbench.bean.SwbAuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.authorityInfo.service.AuthorityInfo;
import com.goldgov.authorityInfo.service.AuthorityInfoExt;
import com.goldgov.authorityInfo.service.AuthorityInfoExtQuery;
import com.goldgov.authorityInfo.service.AuthorityInfoInit;
import com.goldgov.authorityInfo.service.AuthorityInfoQuery;
import com.goldgov.authorityInfo.service.AuthorityInfoService;
import com.goldgov.kcloud.core.json.JsonErrorObject;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.utils.PropertyUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/workbench")
@Api("权限管理服务接口")
public class AuthorityInfoController {
	
	@Autowired
	private AuthorityInfoService service;
	
	@PostMapping("/authorityInfo/settingAuthorityInfo")
	@ApiOperation(value = "设置权限分配", notes ="权限分配")
	@ApiImplicitParams({@ApiImplicitParam(name = "orgScopeStr", value = "管理范围和功能点集合", paramType = "query"),
		@ApiImplicitParam(name = "searchUserID", value = "党员ID", paramType = "query")})
	public JsonObject<Object> settingAuthorityInfo(@ApiIgnore AuthorityInfoQuery query,HttpServletRequest request){
		SwbAuthUser u=LoginAuthUser.getUserByAdminTest(request);
		query.setCurrentUserID(u.getEntityID());
		service.settingAuthorityInfo(query);
		return JsonSuccessObject.SUCCESS;
	}
	
	@PostMapping("/authorityInfo/settingAdmin")
	@ApiOperation(value = "设置管理员")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchUserID", value = "党员ID", paramType = "query")})
	public JsonObject<Object> settingAdmin(@ApiIgnore AuthorityInfoQuery query,HttpServletRequest request){
		service.settingAdminAndScope(query);
		return JsonSuccessObject.SUCCESS;
	}
	
	@DeleteMapping("/authorityInfo/cancelAdmin")
	@ApiOperation(value = "取消管理员&&绑定管理员")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchUserID", value = "党员ID", paramType = "query")})
	public JsonObject<Object> cancelAdmin(@ApiIgnore AuthorityInfoQuery query,HttpServletRequest request){
		if(PropertyUtil.objectNotEmpty(query.getSearchUserID())) {
			service.cancelAdmin(query);
			service.clearAuthorityInfo(query);
			return JsonSuccessObject.SUCCESS;
		}else {
			JsonErrorObject.ERROR.setData("缺少参数，请联系运维人员！");
			return JsonErrorObject.ERROR;
		}
	}
	
	@GetMapping("/open/authorityInfo/findAuthorityInfo")
	@ApiOperation(value = "查看权限分配", notes ="查看")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchUserID", value = "被操作党员ID", paramType = "query")})
	public JsonQueryObject<AuthorityInfoExt> findAuthorityInfo(@ApiIgnore AuthorityInfoExtQuery query,HttpServletRequest request){
		//查询权限列表
		query.setResultList(service.findAuthorityInfo(query));
		return new JsonQueryObject<>(query);
	}
	
	@PostMapping("/authorityInfo/bindingAdmin")
	@ApiOperation(value = "绑定管理员")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchUserID", value = "党员ID", paramType = "query"),
		@ApiImplicitParam(name = "bindingUserID", value = "绑定的管理员ID", paramType = "query")})
	public JsonObject<Object> bindingAdmin(@ApiIgnore AuthorityInfoQuery query,HttpServletRequest request){
		if(PropertyUtil.objectNotEmpty(query.getSearchUserID())&&PropertyUtil.objectNotEmpty(query.getBindingUserID())) {
			service.bindingAdmin(query);
			return JsonSuccessObject.SUCCESS;
		}else {
			JsonErrorObject.ERROR.setData("缺少参数，请联系运维人员！");
			return JsonErrorObject.ERROR;
		}
	}
	
	@GetMapping("/open/authorityInfo/findAdminList")
	@ApiOperation(value = "查询管理员列表")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchOrgID", value = "机构ID", paramType = "query"),
		@ApiImplicitParam(name = "searchUserID", value = "党员ID", paramType = "query"),
		@ApiImplicitParam(name = "searchLoginID", value = "登录ID", paramType = "query"),
		@ApiImplicitParam(name = "searchIsBindingAdmin", value = "是否查询绑定管理员", paramType = "query")
	})
	public JsonQueryObject<AuthorityInfo> findAdminList(@ApiIgnore AuthorityInfoQuery query,HttpServletRequest request){
		if(query.getSearchIsBindingAdmin()==0||query.getSearchIsBindingAdmin()==1) {
			//默认不查询绑定管理员
			query.setSearchIsBindingAdmin(1);
		}
		query.setResultList(service.findAdminList(query));
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/authorityInfo/findFunctionList")
	@ApiOperation(value = "查询功能列表")
	public JsonObject<Object> findFunctionList(HttpServletRequest request){
		Map<String,String> map=AuthorityInfoInit.getFunctionMap();
		List<AuthorityInfo> authDataList = new ArrayList<>();
		Iterator<Entry<String, String>> it = map.entrySet().iterator();
	    while (it.hasNext()) {
		    Entry<String, String> entry = it.next();
		    authDataList.add(new AuthorityInfo(entry.getKey(), entry.getValue()));
	    }
		JsonSuccessObject.SUCCESS.setData(authDataList);
		return JsonSuccessObject.SUCCESS;
	}
}